
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, FlatList, Image, TouchableOpacity} from 'react-native';
import {Util} from "../util";
import TwebView from '../twebView'
const instructions = Platform.select({
    ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
    android:
        'Double tap R on your keyboard to reload,\n' +
        'Shake or press menu button for dev menu',
});

type Props = {};
export default class list extends Component<Props> {
    _keyExtractor = (item, index) => item.id;
    static  navigationOptions=({navigation,navigationOptions}) => {
      return {
          title: navigation.getParam('title'),
      };
    };
    constructor(props){
        super(props);
        const {navigation} = props;
        let url = navigation.getParam('url');
        this.state={
            navigation: navigation,
            url: url,
            dataSource:[]
        }
    }

    render() {
        return (
            <FlatList
                data={this.state.dataSource}
                renderItem={({item}) => this._renderItemView(item)}
                keyExtractor={this._keyExtractor}
            />

        );
    }
    _showWebPage(url,title){
        this.state.navigation.navigate('TwebView',{url: url, title: title});
    }
    _renderItemView(item){
        return (
            (
                <TouchableOpacity style={styles.item} onPress={this._showWebPage.bind(this,item.url,item.title)}>
                    <View>
                        <Image style={styles.img} source={{uri : item.img}}/>
                    </View>
                    <View style={styles.text_wraper}>
                        <Text style={styles.title} numberOfLines={1}>{item.title}</Text>
                        <Text style={styles.time}>{item.time}</Text>
                    </View>
                </TouchableOpacity>
            )
        )
    }

    componentDidMount(){
        let url = this.state.url;
        let that = this;
        Util.get(url,function (data) {
            if (data.status === 1){
                console.log(data);
                that.setState({
                   dataSource: data.data,
                });
            }else {
                alert('数据获取失败');
            }
        },function (err) {

        })
    }


}

const styles = StyleSheet.create({
    item:{
      height: 77,
        paddingLeft:10,
        paddingRight: 10,
        borderWidth: Util.pixel,
        borderBottomColor: '#EDEDED',
        flexDirection: 'row',
    },
    img:{
      height: 60,
      width: 60,
        borderRadius: 3,
        marginTop: 5,
    },
    text_wraper:{
        marginLeft: 10,
        flex: 1,
    },
    title: {
        fontSize: 16,
        marginTop: 10,
    },
    time: {
        color: '#DDD',
        fontSize: 13,
        marginTop: 5,
    }
});